// Copyright 2012 Square, Inc.
package com.kanche.bigbaby.collection;

import android.content.Context;
import android.content.Intent;

import com.google.gson.Gson;
import com.squareup.otto.Bus;
import com.squareup.otto.Produce;
import com.squareup.tape.InMemoryObjectQueue;
import com.squareup.tape.ObjectQueue;
import com.squareup.tape.TaskQueue;

public class ImageUploadTaskQueue extends TaskQueue<ImageUploadTask> {
    private static final String FILENAME = "image_upload_task_queue";

    private final Context context;
    private final Bus bus;

    private ImageUploadTaskQueue(ObjectQueue<ImageUploadTask> delegate, Context context, Bus bus) {
        super(delegate);
        this.context = context;
        this.bus = bus;
        try {
            bus.register(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (size() > 0) {
            startService();
        }
    }

    public static ImageUploadTaskQueue create(Context context, Bus bus) {
//        Converter<ImageUploadTask> converter = new GsonConverter<ImageUploadTask>(gson, ImageUploadTask.class);
//        File queueFile = new File(context.getFilesDir(), FILENAME);
//        FileObjectQueue<ImageUploadTask> delegate;
//        try {
//            delegate = new FileObjectQueue<ImageUploadTask>(queueFile, converter);
//        } catch (IOException e) {
//            throw new RuntimeException("Unable to create file queue.", e);
//        }
        ObjectQueue<ImageUploadTask> delegate = new InMemoryObjectQueue<ImageUploadTask>();
        ImageUploadTaskQueue queue = new ImageUploadTaskQueue(delegate, context, bus);
        return queue;
    }

    public static ImageUploadTaskQueue create(Context context, Gson gson, Bus bus) {
//        Converter<ImageUploadTask> converter = new GsonConverter<ImageUploadTask>(gson, ImageUploadTask.class);
//        File queueFile = new File(context.getFilesDir(), FILENAME);
//        FileObjectQueue<ImageUploadTask> delegate;
//        try {
//            delegate = new FileObjectQueue<ImageUploadTask>(queueFile, converter);
//        } catch (IOException e) {
//            throw new RuntimeException("Unable to create file queue.", e);
//        }
        ObjectQueue<ImageUploadTask> delegate = new InMemoryObjectQueue<ImageUploadTask>();
        ImageUploadTaskQueue queue = new ImageUploadTaskQueue(delegate, context, bus);
        return queue;
    }

    private void startService() {
        context.startService(new Intent(context, ImageUploadTaskService.class));
    }

    @Override
    public void add(ImageUploadTask entry) {
        super.add(entry);
        bus.post(produceSizeChanged());
        startService();
    }

    @Override
    public void remove() {
        super.remove();
        bus.post(produceSizeChanged());
    }

    @SuppressWarnings("UnusedDeclaration") // Used by event bus.
    @Produce
    public ImageUploadQueueSizeEvent produceSizeChanged() {
        return new ImageUploadQueueSizeEvent(size());
    }
}
